#include "../source/log.h"
#include "../source/hook.h"
#include "../source/io_manager.h"
#include <cstdio>
#include <iostream>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
#include <sys/stat.h>


void test_sleep()
{
    // set_log_level(log_level::warning);

    IOManager iom(1);
    iom.schedule([](){
        sleep(2);
        log_error("test sleep 2s");
    });

    iom.schedule([](){
        sleep(3);
        log_error("test sleep 3s");
    });

    log_error("test sleep begin: ");
}

void test_sock()
{
    int sock = socket(AF_INET, SOCK_STREAM, 0);

    sockaddr_in addr;
    memset(&addr, 0, sizeof(sockaddr_in));
    addr.sin_family = AF_INET;
    addr.sin_port = htons(80);
    inet_pton(AF_INET, "182.61.200.6", &addr.sin_addr.s_addr); //百度的ip
    
    int rt = connect(sock, (const sockaddr*)&addr, sizeof(sockaddr_in));
    if(rt)
    {
        return;
    }

    const char data[] = "GET / HTTP/1.0\r\n\r\n";
    rt = send(sock, data, sizeof(data), 0);
    if(rt <= 0)
    {
        return;
    }

    std::string buff;
    buff.resize(8184);
    rt = recv(sock, &buff[0], buff.size(), 0);
    if(rt <= 0)
    {
        return;
    }

    buff.reserve(rt);
    std::cout << buff;
}

int main(void)
{
    // test_sleep();

    IOManager iom(1);
    iom.schedule(test_sock);
    // test_sock();

    return 0;
}